package gov.mmgt.netmanager.ui;

import gov.mmgt.netmanager.common.Constant;
import gov.mmgt.netmanager.common.exception.MyException;
import gov.mmgt.netmanager.operate.Login;
import gov.mmgt.netmanager.operate.PropertiesOperate;
import gov.mmgt.netmanager.operate.TestDatabase;
import gov.mmgt.netmanager.ui.common.MyDialog;
import gov.mmgt.netmanager.ui.util.UIUtil;

import java.io.InputStream;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginUI {

	private Logger log = Logger.getLogger(this.getClass());
	
	protected Shell shell;
	protected Label smallBtn;
	protected Label closeBtn;
	private Label userIcon;
	private Text account;
	private Label passwordIcon;
	private Text password;
	private Button loginBtn;
	private Button netConnectBtn;
	
	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginUI window = new LoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		
		// 鼠标拖动
		UIUtil.drag(shell);
		
		// 读取配置文件
		String file = "config.ini";
		
		PropertiesOperate readProperties = new PropertiesOperate(file);
		
		try {
			readProperties.getParameter();
		} catch (MyException e) {
			e.printStackTrace();
		}

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NO_TRIM);
		shell.setSize(567, 298);
		shell.setText("茂名市国土资源局网络管理工具");
		InputStream is = this.getClass().getResourceAsStream(
				"images/loginUI.png");
		shell.setBackgroundImage(new Image(Display.getDefault(), is));
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		
		//设置程序图标
		is = this.getClass().getResourceAsStream("images/ico.png");
		Image image = new Image(Display.getDefault(), is);
		shell.setImage(image);

		// 窗口居中
		UIUtil.moveWinToMiddle(shell);
		
		//设置快捷键
		Display.getDefault().addFilter(SWT.KeyDown, new Listener() {
			@Override
			public void handleEvent(Event e) {
				if(e.keyCode == 13 || e.keyCode == 16777296){
					login();
				}
			}
		});
		
		// ===============================最小化按钮===============================//
		smallBtn = new Label(shell, SWT.NONE);
		
		smallBtn.setToolTipText("最小化");

		smallBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/mini_on.png");
				smallBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/mini_off.png");
				smallBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		smallBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		
		smallBtn.setBounds(495, 0, 31, 21);
		is = this.getClass().getResourceAsStream("images/mini_off.png");
		smallBtn.setImage(new Image(Display.getDefault(), is));
		// ===============================最小化按钮===============================//

		// ===============================关闭按钮===============================//
		closeBtn = new Label(shell, SWT.NONE);
		closeBtn.setToolTipText("关闭");

		closeBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_on.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/close_off.png");
				closeBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		closeBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.close();
			}
		});

		closeBtn.setBounds(528, 0, 31, 21);
		is = this.getClass().getResourceAsStream("images/close_off.png");
		closeBtn.setImage(new Image(Display.getDefault(), is));
		// ===============================关闭按钮===============================//

		userIcon = new Label(shell, SWT.NONE);
		userIcon.setBounds(310, 122, 32, 32);

		is = this.getClass().getResourceAsStream("images/user.png");
		userIcon.setImage(new Image(Display.getDefault(), is));

		account = new Text(shell, SWT.NONE);
		account.setBounds(348, 126, 163, 23);
		account.setFocus();

		is = this.getClass().getResourceAsStream("images/input.png");
		account.setBackgroundImage(new Image(Display.getDefault(), is));

		passwordIcon = new Label(shell, SWT.NONE);
		passwordIcon.setBounds(310, 160, 32, 32);

		is = this.getClass().getResourceAsStream("images/password.png");
		passwordIcon.setImage(new Image(Display.getDefault(), is));

		password = new Text(shell, SWT.PASSWORD);
		password.setBounds(348, 165, 163, 23);

		is = this.getClass().getResourceAsStream("images/input.png");
		password.setBackgroundImage(new Image(Display.getDefault(), is));

		// ===============================登录按钮===============================//
		loginBtn = new Button(shell, SWT.NONE);
		loginBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				login();
			}
		});

		loginBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/btn_on.png");
				loginBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/btn_off.png");
				loginBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		loginBtn.setBounds(310, 200, 80, 26);

		is = this.getClass().getResourceAsStream("images/btn_off.png");
		loginBtn.setImage(new Image(Display.getDefault(), is));
		// ===============================登录按钮===============================//

		// ===============================设置数据源按钮===============================//
		netConnectBtn = new Button(shell, SWT.NONE);
		netConnectBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NetConnDialog netConnDialog = new NetConnDialog(shell, SWT.NONE);
				netConnDialog.open();
			}
		});

		netConnectBtn.addMouseTrackListener(new MouseTrackAdapter() {
			// 鼠标在按钮上的事件
			@Override
			public void mouseEnter(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/data_source_on.png");
				netConnectBtn.setImage(new Image(Display.getDefault(), is));
			}

			// 鼠标离开事件
			@Override
			public void mouseExit(MouseEvent e) {
				InputStream is = this.getClass().getResourceAsStream(
						"images/data_source_off.png");
				netConnectBtn.setImage(new Image(Display.getDefault(), is));
			}
		});

		netConnectBtn.setBounds(415, 200, 97, 26);

		is = this.getClass().getResourceAsStream("images/data_source_off.png");
		netConnectBtn.setImage(new Image(Display.getDefault(), is));
		// ===============================设置数据源按钮===============================//
	}
	
	/**
	 * 
	 * 方法概述：登录操作
	 * 描述：登录操作
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2014-5-19
	 */
	private void login() {
		//判断是否设置了数据源
		if(Constant.PARAMETER.getDataSource().isEmpty()) {
			MyDialog.alert(shell, "请先设置数据源");
		} else {
			//表单验证
			if(account.getText().isEmpty()) {
				MyDialog.alert(shell, "请输入用户名！");
				account.setFocus();
				return;
			}
			
			if(password.getText().isEmpty()) {
				MyDialog.alert(shell, "请输入密码！");
				password.setFocus();
				return;
			}
			
			//测试数据源是否正确
			try {
				log.debug("测试数据源是否正确");
				TestDatabase.testDatabase(Constant.PARAMETER.getDataSource());
			} catch (MyException e1) {
				MyDialog.alert(shell, e1.getMessage());
			}
			
			//登录系统
			try {
				log.debug("登录系统");
				Login login = new Login();
				login.login(account.getText(), password.getText());
				Constant.IS_LOGIN = true;
				shell.close();
				MainUI main = new MainUI();
				main.open();
			} catch (MyException e1) {
				MyDialog.alert(shell, e1.getMessage());
			}
		}
	}
}
